﻿using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Com.Seezt.Skins;
using Components;
using UI.Lib;
using UI.Lib.Factory;

namespace UI.Invoice
{
    public partial class FrmTic1a00Medit : UI.ParentForm.BaseMedit
    {
        public FrmTic1a00Medit(string modulecode, string action, string text, string key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            this.Key = key;

            //回写的方法
            this.SaveCallFuntion = "Tic1a00Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.MaxStateFlag = true;
            this.allowDtlEmpty = false; //可能用户一次想把全部的记录删除
            if (!string.IsNullOrEmpty(this.Key))
            {
                LoadData();
                IsAfterSaveCloseWin = true;
            }
        }

        protected override void PageInit()
        {
        }

        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.listid ='{0}' ", this.Key));
            base.LoadDtlData(null, string.Format(" a.plistid ='{0}' ", this.Key));
        }

        protected override bool Check()
        {
            var subCodes = new List<string>();
            foreach (DataGridViewRow dgRow in dgvDtl.Rows)
            {
                subCodes.Add(Validation.ToString(dgRow.Cells["subcode"].Value));
            }

            //获取数据库的subcode
            var tbList = CommonHelper.Entity.ExecFun("getsubcodelist", QB.Entity.AddParam("appid", Context.realComcode));
            if (null != tbList)
            {
                var findCode = new List<string>();
                foreach (DataRow row in tbList.Rows)
                {
                    var code = Validation.ToString(row[0]);

                    if (subCodes.IndexOf(code) > -1)
                        findCode.Add(code);
                }

                if (findCode.Count > 0)
                {
                    MsgBox.Show(this, "提示错误", string.Format("编码{0} 已存在，请调整", string.Join("、", findCode.ToArray())), MessageBoxIcon.Error);
                    return false;
                }
            }

            return true;
        }
    }
}
